import { getStore } from "@/store";
import type { User, Customer } from "../types/user";

import { request } from "@/service/request/request";
import { persistToken } from "./auth";

/**
 * 用户登陆
 * @param data { account: string; password: string }
 */
export const login = async (data: { account: string; password: string }) => {
    try {
        const userStore = getStore("user");
        const { user, token } = await request<{
            user: User | Customer;
            token: string;
        }>("/user/login_user", "post", data);

        persistToken(token); // 持久化 token
        userStore.setUserInfo(user); // 设置用户信息
        userStore.setIsAuthenticated(true); // 设置认证状态为 true
    } catch (error) {
        throw error;
    }
};

/**
 * 查询客户注册状态
 * @param account 查询账号
 * @return {status} 0: 被拒绝 1: 已通过 2: 待审核
 * @return {comments} 被拒绝的原因
 */
export const checkRegisterStatus = async (account: string) => {
    try {
        return await request<number | { status: number; comments: string }>(
            `user/customer/inquire/${account}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 重置密码
 * @param account 账号
 * @param password 新密码
 * @param verificationCode 验证码
 * @returns 0: 成功 1: 失败
 */
export const resetPassword = async (
    account: string,
    password: string,
    verificationCode: string
) => {
    try {
        return await request<number>("/user/user_reset_password", "post", {
            account: account,
            password: password,
            verificationCode: verificationCode,
        });
    } catch (error) {
        throw error;
    }
};

export const updateUserAvatar = async (data: FormData) => {
    try {
        return await request<string>("/user/set_profilePicture", "post", data, {
            headers: { "Content-Type": "multipart/form-data" },
        });
    } catch (error) {
        throw error;
    }
};

/**
 * 根据id查询用户信息
 * @param id
 * @returns
 */
export const getUserById = async (id: string) => {
    try {
        return await request<User | Customer>(
            `/user/get_user/${id}`,
            "post",
            null
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 添加用户 (管理员) -> 添加客户或员工
 * @param user
 * @returns
 */
export const addUser = async (user: User | Customer) => {
    try {
        return await request<User | Customer>("/user/add_user", "post", user);
    } catch (error) {
        throw error;
    }
};

/**
 * 更新用户信息 (管理员) -> 更新客户或员工
 * @param user
 * @returns
 */
export const updateUser = async (user: User | Customer) => {
    try {
        return await request<User | Customer>(
            "/user/update_user",
            "post",
            user
        );
    } catch (error) {
        throw error;
    }
};

/**
 * 删除用户 (管理员) -> 删除客户或员工, 支持批量删除
 * @param ids  用户id数组
 * @returns
 */
export const deleteUser = async (ids: string[]) => {
    try {
        return await request<number>(`/user/delete_user`, "post", {
            userIdList: ids,
        });
    } catch (error) {
        throw error;
    }
};
